package com.ruoyi.web.controller.business;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Userinfo;
import com.ruoyi.system.service.IUserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 患者档案Controller
 * 
 * @author gzhuangwei
 * @date 2022-09-17
 */
@Api("患者信息管理")
@RestController
@RequestMapping("/business/userinfo")
public class UserinfoController extends BaseController
{
    @Autowired
    private IUserinfoService userinfoService;

    /**
     * 查询患者档案列表
     */
    @PreAuthorize("@ss.hasPermi('business:userinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(Userinfo userinfo)
    {
        startPage();
        List<Userinfo> list = userinfoService.selectUserinfoList(userinfo);
        return getDataTable(list);
    }

    /**
     * 导出患者档案列表
     */
    @PreAuthorize("@ss.hasPermi('business:userinfo:export')")
    @Log(title = "患者档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Userinfo userinfo)
    {
        List<Userinfo> list = userinfoService.selectUserinfoList(userinfo);
        ExcelUtil<Userinfo> util = new ExcelUtil<Userinfo>(Userinfo.class);
        util.exportExcel(response, list, "患者档案数据");
    }

    /**
     * 患者档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:userinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userinfoService.selectUserinfoById(id));
    }
    /**
     *  1.1.1
     * 新增患者档案
     */
    @ApiOperation("新增患者档案")
    @Log(title = "患者档案", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Userinfo userinfo)
    {
        // 创建日期Date对象
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        //设置注册时间
        userinfo.setRegdate(timeStamp);
        return toAjax(userinfoService.insertUserinfo(userinfo));
    }

    /**
     * 1.1.2
     * 通过ID获取患者档案详细信息
     */
    @ApiOperation("通过userId获取患者档案详细信息")
    @GetMapping(value = "/getByUserId")
    public AjaxResult getByUserId(@RequestParam("userId") String userId)
    {
        return AjaxResult.success(userinfoService.selectUserInfoByUserId(userId));
    }

    /**
     * 1.4.3
     * 患者绑定医生，修改患者表的doctorId
     */
    @ApiOperation("患者绑定医生")
    @GetMapping(value = "/editDoctorId")
    public AjaxResult editDoctorId(@RequestParam(name = "userId")String userId , @RequestParam("doctorId") String doctorId)
    {
        //通过userId查询
        Userinfo userinfo = userinfoService.selectUserInfoByUserId(userId);
        //修改doctorId
        userinfo.setDoctorid(doctorId);

        return toAjax(userinfoService.updateUserinfo(userinfo));
    }
    /**
     * 修改患者档案
     */
    @PreAuthorize("@ss.hasPermi('business:userinfo:edit')")
    @Log(title = "患者档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Userinfo userinfo)
    {
        return toAjax(userinfoService.updateUserinfo(userinfo));
    }

    /**
     * 删除患者档案
     */
    @PreAuthorize("@ss.hasPermi('business:userinfo:remove')")
    @Log(title = "患者档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userinfoService.deleteUserinfoByIds(ids));
    }
}
